package com.sunwayworld.basemodule.common.auditrecord.service.impl;

import com.sunwayworld.basemodule.common.auditrecord.bean.LimsAuditRecordBean;
import com.sunwayworld.basemodule.common.auditrecord.dao.LimsAuditRecordDao;
import com.sunwayworld.basemodule.common.auditrecord.service.LimsAuditRecordService;
import com.sunwayworld.framework.at.annotation.AuditTrailEntry;
import com.sunwayworld.framework.at.annotation.AuditTrailType;
import com.sunwayworld.framework.context.ApplicationContextHelper;
import com.sunwayworld.framework.context.LocalContextHelper;
import com.sunwayworld.framework.restful.data.RestJsonWrapperBean;
import com.sunwayworld.framework.spring.annotation.GikamBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

@Repository
@GikamBean
public class LimsAuditRecordServiceImpl implements LimsAuditRecordService {
    @Autowired
    protected LimsAuditRecordDao auditRecordDao;

    @Lazy
    @Autowired
    private LimsAuditRecordService recordService;

    @Override
    @SuppressWarnings("unchecked")
    public LimsAuditRecordDao getDao() {
        return auditRecordDao;
    }

    @Override
    @Transactional
    @AuditTrailEntry(AuditTrailType.INSERT)
    public Long insert(RestJsonWrapperBean jsonWrapper) {
        List<LimsAuditRecordBean> auditRecordBeans = jsonWrapper.parse(LimsAuditRecordBean.class);
        auditRecordBeans.forEach(auditRecord -> {
            auditRecord.setId(ApplicationContextHelper.getNextIdentity());
            auditRecord.setAuditById(LocalContextHelper.getLoginUserId());
            auditRecord.setAuditByName(LocalContextHelper.getLoginUserName());
            getDao().insert(auditRecord);
        });
        return auditRecordBeans.get(0).getId();
    }

    @Override
    public LimsAuditRecordBean insertAuditRecord(Long businessId, String businessCode, String auditStep, String auditOperation, String remarks) {
        LimsAuditRecordBean au = new LimsAuditRecordBean();
        au.setId(ApplicationContextHelper.getNextIdentity());
        au.setBusinessId(businessId.toString());
        au.setBusinessCode(businessCode);
        au.setAuditStep(auditStep);
        au.setAuditOperation(auditOperation);
        au.setRemarks(remarks);
        au.setCreatedById(LocalContextHelper.getLoginUserId());
        au.setCreatedByName(LocalContextHelper.getLoginUserName());
        au.setAuditById(LocalContextHelper.getLoginUserId());
        au.setAuditByName(LocalContextHelper.getLoginUserName());
        getDao().insert(au);
        return au;
    }

    @Transactional
    @Override
    public void insertAuditRecords(List<Long> businessIds, String businessCode, String auditStep, String auditOperation, String remarks) {
        List<LimsAuditRecordBean> auditRecordBeans = new ArrayList<>();
        businessIds.forEach(id -> {
            LimsAuditRecordBean au = new LimsAuditRecordBean();
            au.setBusinessId(id.toString());
            au.setBusinessCode(businessCode);
            au.setAuditStep(auditStep);
            au.setAuditOperation(auditOperation);
            au.setRemarks(remarks);
            au.setCreatedById(LocalContextHelper.getLoginUserId());
            au.setCreatedByName(LocalContextHelper.getLoginUserName());
            au.setAuditById(LocalContextHelper.getLoginUserId());
            au.setAuditByName(LocalContextHelper.getLoginUserName());
            auditRecordBeans.add(au);
        });
        recordService.insertRecords(auditRecordBeans);// 需要这样才能异步
    }

    @Async
    @Override
    public void insertRecords(List<LimsAuditRecordBean> auditRecordBeans) {
        if (!auditRecordBeans.isEmpty()) {
            List<Long> nextIdentityList = ApplicationContextHelper.getNextIdentityList(auditRecordBeans.size());
            AtomicInteger index = new AtomicInteger(0);
            auditRecordBeans.forEach(a -> a.setId(nextIdentityList.get(index.getAndIncrement())));
            getDao().fastInsert(auditRecordBeans);
        }
    }

    @Override
    public void deleteByBusinessId(Long businessId) {
        getDao().deleteByBusinessId(businessId);
    }
}
